package com.ibm.risk.irmp.workflow.client.flowable.api;

import com.ibm.risk.irmp.workflow.client.flowable.ApiClient;

import com.ibm.risk.irmp.workflow.client.flowable.model.RestVariable;
import com.ibm.risk.irmp.workflow.client.flowable.model.TaskVariableCollectionResource;
import com.ibm.risk.irmp.workflow.client.flowable.model.TaskVariableResource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;


@Component("com.ibm.risk.irmp.flowable.api.TaskVariablesApi")
public class TaskVariablesApi {
    private ApiClient apiClient;

    public TaskVariablesApi() {
        this(new ApiClient());
    }

    @Autowired
    public TaskVariablesApi(ApiClient apiClient) {
        this.apiClient = apiClient;
    }

    public ApiClient getApiClient() {
        return apiClient;
    }

    public void setApiClient(ApiClient apiClient) {
        this.apiClient = apiClient;
    }

    /**
     * Create new variables on a task
     * This endpoint can be used in 2 ways: By passing a JSON Body (RestVariable or an Array of RestVariable) or by passing a multipart/form-data Object. It is possible to create simple (non-binary) variable or list of variables or new binary variable  Any number of variables can be passed into the request body array. NB: Swagger V2 specification does not support this use case that is why this endpoint might be buggy/incomplete if used with other tools.
     * <p><b>200</b> - successful operation
     * <p><b>201</b> - Indicates the variables were created and the result is returned.
     * <p><b>400</b> - Indicates the name of a variable to create was missing or that an attempt is done to create a variable on a standalone task (without a process associated) with scope global or an empty array of variables was included in the request or request did not contain an array of variables. Status message provides additional information.
     * <p><b>404</b> - Indicates the requested task was not found.
     * <p><b>409</b> - Indicates the task already has a variable with the given name. Use the PUT method to update the task variable instead.
     * <p><b>415</b> - Indicates the serializable data contains an object for which no class is present in the JVM running the Flowable engine and therefore cannot be deserialized.
     * @param taskId The taskId parameter
     * @param body Create a variable on a task
     * @param name Required name of the variable
     * @param type Type of variable that is created. If omitted, reverts to raw JSON-value type (string, boolean, integer or double)
     * @param scope Scope of variable that is created. If omitted, local is assumed.
     * @return Object
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public Object createTaskVariable(String taskId, TaskVariableCollectionResource body, String name, String type, String scope) throws RestClientException {
        Object postBody = body;

        // verify the required parameter 'taskId' is set
        if (taskId == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'taskId' when calling createTaskVariable");
        }

        // create path and map variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("taskId", taskId);
        String path = UriComponentsBuilder.fromPath("/runtime/tasks/{taskId}/variables").buildAndExpand(uriVariables).toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        if (name != null)
            formParams.add("name", name);
        if (type != null)
            formParams.add("type", type);
        if (scope != null)
            formParams.add("scope", scope);

        final String[] accepts = {
            "application/json"
        };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = {
            "application/json", "multipart/form-data"
        };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<Object> returnType = new ParameterizedTypeReference<Object>() {};
        return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
    /**
     * Delete a variable on a task
     *
     * <p><b>204</b> - Indicates the task variable was found and has been deleted. Response-body is intentionally empty.
     * <p><b>404</b> - Indicates the requested task was not found or the task does not have a variable with the given name. Status message contains additional information about the error.
     * @param taskId The taskId parameter
     * @param variableName The variableName parameter
     * @param scope Scope of variable to be returned. When local, only task-local variable value is returned. When global, only variable value from the task’s parent execution-hierarchy are returned. When the parameter is omitted, a local variable will be returned if it exists, otherwise a global variable.
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public void deleteTaskInstanceVariable(String taskId, String variableName, String scope) throws RestClientException {
        Object postBody = null;

        // verify the required parameter 'taskId' is set
        if (taskId == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'taskId' when calling deleteTaskInstanceVariable");
        }

        // verify the required parameter 'variableName' is set
        if (variableName == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'variableName' when calling deleteTaskInstanceVariable");
        }

        // create path and map variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("taskId", taskId);
        uriVariables.put("variableName", variableName);
        String path = UriComponentsBuilder.fromPath("/runtime/tasks/{taskId}/variables/{variableName}").buildAndExpand(uriVariables).toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "scope", scope));

        final String[] accepts = { };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<Void> returnType = new ParameterizedTypeReference<Void>() {};
        apiClient.invokeAPI(path, HttpMethod.DELETE, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
    /**
     * Get a variable from a task
     *
     * <p><b>200</b> - Indicates the task was found and the requested variables are returned.
     * <p><b>404</b> - Indicates the requested task was not found or the task does not have a variable with the given name (in the given scope). Status message provides additional information.
     * @param taskId The taskId parameter
     * @param variableName The variableName parameter
     * @param scope Scope of variable to be returned. When local, only task-local variable value is returned. When global, only variable value from the task’s parent execution-hierarchy are returned. When the parameter is omitted, a local variable will be returned if it exists, otherwise a global variable.
     * @return RestVariable
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public RestVariable getTaskInstanceVariable(String taskId, String variableName, String scope) throws RestClientException {
        Object postBody = null;

        // verify the required parameter 'taskId' is set
        if (taskId == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'taskId' when calling getTaskInstanceVariable");
        }

        // verify the required parameter 'variableName' is set
        if (variableName == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'variableName' when calling getTaskInstanceVariable");
        }

        // create path and map variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("taskId", taskId);
        uriVariables.put("variableName", variableName);
        String path = UriComponentsBuilder.fromPath("/runtime/tasks/{taskId}/variables/{variableName}").buildAndExpand(uriVariables).toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "scope", scope));

        final String[] accepts = {
            "application/json"
        };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<RestVariable> returnType = new ParameterizedTypeReference<RestVariable>() {};
        return apiClient.invokeAPI(path, HttpMethod.GET, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
    /**
     * Get the binary data for a variable
     * The response body contains the binary value of the variable. When the variable is of type binary, the content-type of the response is set to application/octet-stream, regardless of the content of the variable or the request accept-type header. In case of serializable, application/x-java-serialized-object is used as content-type.
     * <p><b>200</b> - Indicates the task was found and the requested variables are returned.
     * <p><b>404</b> - Indicates the requested task was not found or the task does not have a variable with the given name (in the given scope). Status message provides additional information.
     * @param taskId The taskId parameter
     * @param variableName The variableName parameter
     * @param scope Scope of variable to be returned. When local, only task-local variable value is returned. When global, only variable value from the task’s parent execution-hierarchy are returned. When the parameter is omitted, a local variable will be returned if it exists, otherwise a global variable.
     * @return List&lt;byte[]&gt;
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public List<byte[]> getTaskVariableData(String taskId, String variableName, String scope) throws RestClientException {
        Object postBody = null;

        // verify the required parameter 'taskId' is set
        if (taskId == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'taskId' when calling getTaskVariableData");
        }

        // verify the required parameter 'variableName' is set
        if (variableName == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'variableName' when calling getTaskVariableData");
        }

        // create path and map variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("taskId", taskId);
        uriVariables.put("variableName", variableName);
        String path = UriComponentsBuilder.fromPath("/runtime/tasks/{taskId}/variables/{variableName}/data").buildAndExpand(uriVariables).toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "scope", scope));

        final String[] accepts = {
            "application/json"
        };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<List<byte[]>> returnType = new ParameterizedTypeReference<List<byte[]>>() {};
        return apiClient.invokeAPI(path, HttpMethod.GET, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
    /**
     * List variables for a task
     *
     * <p><b>200</b> - Indicates the task was found and the requested variables are returned
     * <p><b>404</b> - Indicates the requested task was not found..
     * @param taskId The taskId parameter
     * @param scope Scope of variable to be returned. When local, only task-local variable value is returned. When global, only variable value from the task’s parent execution-hierarchy are returned. When the parameter is omitted, a local variable will be returned if it exists, otherwise a global variable.
     * @return List&lt;RestVariable&gt;
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public List<RestVariable> listTaskVariables(String taskId, String scope) throws RestClientException {
        Object postBody = null;

        // verify the required parameter 'taskId' is set
        if (taskId == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'taskId' when calling listTaskVariables");
        }

        // create path and map variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("taskId", taskId);
        String path = UriComponentsBuilder.fromPath("/runtime/tasks/{taskId}/variables").buildAndExpand(uriVariables).toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "scope", scope));

        final String[] accepts = {
            "application/json"
        };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<List<RestVariable>> returnType = new ParameterizedTypeReference<List<RestVariable>>() {};
        return apiClient.invokeAPI(path, HttpMethod.GET, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
    /**
     * Update an existing variable on a task
     * This endpoint can be used in 2 ways: By passing a JSON Body (RestVariable) or by passing a multipart/form-data Object. It is possible to update simple (non-binary) variable or  binary variable  Any number of variables can be passed into the request body array. NB: Swagger V2 specification does not support this use case that is why this endpoint might be buggy/incomplete if used with other tools.
     * <p><b>200</b> - Indicates the variables was updated and the result is returned.
     * <p><b>400</b> - Indicates the name of a variable to update was missing or that an attempt is done to update a variable on a standalone task (without a process associated) with scope global. Status message provides additional information.
     * <p><b>404</b> - Indicates the requested task was not found or the task does not have a variable with the given name in the given scope. Status message contains additional information about the error.
     * <p><b>415</b> - Indicates the serializable data contains an object for which no class is present in the JVM running the Flowable engine and therefore cannot be deserialized.
     * @param taskId The taskId parameter
     * @param variableName The variableName parameter
     * @param body Update a task variable
     * @param name Required name of the variable
     * @param type Type of variable that is updated. If omitted, reverts to raw JSON-value type (string, boolean, integer or double)
     * @param scope Scope of variable to be returned. When local, only task-local variable value is returned. When global, only variable value from the task’s parent execution-hierarchy are returned. When the parameter is omitted, a local variable will be returned if it exists, otherwise a global variable..
     * @return RestVariable
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public RestVariable updateTaskInstanceVariable(String taskId, String variableName, TaskVariableResource body, String name, String type, String scope) throws RestClientException {
        Object postBody = body;

        // verify the required parameter 'taskId' is set
        if (taskId == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'taskId' when calling updateTaskInstanceVariable");
        }

        // verify the required parameter 'variableName' is set
        if (variableName == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'variableName' when calling updateTaskInstanceVariable");
        }

        // create path and map variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("taskId", taskId);
        uriVariables.put("variableName", variableName);
        String path = UriComponentsBuilder.fromPath("/runtime/tasks/{taskId}/variables/{variableName}").buildAndExpand(uriVariables).toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        if (name != null)
            formParams.add("name", name);
        if (type != null)
            formParams.add("type", type);
        if (scope != null)
            formParams.add("scope", scope);

        final String[] accepts = {
            "application/json"
        };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = {
            "application/json", "multipart/form-data"
        };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<RestVariable> returnType = new ParameterizedTypeReference<RestVariable>() {};
        return apiClient.invokeAPI(path, HttpMethod.PUT, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
}
